/*
 * @Author: liangkun 2669681641@qq.com
 * @Date: 2023-08-11 14:23:01
 * @LastEditors: liangkun 2669681641@qq.com
 * @LastEditTime: 2023-11-06 13:57:12
 * @FilePath: \radiation-hygiene-fe\src\store\modules\app.js
 * @Description: 
 * 
 * Copyright (c) 2023 by ${git_name_email}, All Rights Reserved. 
 */
import Cookies from 'js-cookie'

const state = {
  sidebar: {
    opened: Cookies.get('sidebarStatus') ? !!+Cookies.get('sidebarStatus') : true,
    withoutAnimation: false,
    hide: false
  },
  device: 'desktop',
  size: Cookies.get('size') || 'medium',
  app_loading: {
    ide: '',
    show: false
  },
	has_role: true,
  ukey_status: false,
}

const mutations = {
  TOGGLE_SIDEBAR: state => {
    if (state.sidebar.hide) {
      return false;
    }
    state.sidebar.opened = !state.sidebar.opened
    state.sidebar.withoutAnimation = false
    if (state.sidebar.opened) {
      Cookies.set('sidebarStatus', 1)
    } else {
      Cookies.set('sidebarStatus', 0)
    }
  },
  CLOSE_SIDEBAR: (state, withoutAnimation) => {
    Cookies.set('sidebarStatus', 0)
    state.sidebar.opened = false
    state.sidebar.withoutAnimation = withoutAnimation
  },
  TOGGLE_DEVICE: (state, device) => {
    state.device = device
  },
  SET_SIZE: (state, size) => {
    state.size = size
    Cookies.set('size', size)
  },
  SET_SIDEBAR_HIDE: (state, status) => {
    state.sidebar.hide = status
  },
  SET_APP_LOADING: (state, loading) => {
    state.app_loading = {...state.app_loading, ...loading}
  },
  SET_HAS_ROLE: (state, status) => {
    state.has_role = status
  },
  SET_UKEY_STATUS: (state, status) => {
    state.ukey_status = status
  }
}

const actions = {
  toggleSideBar({ commit }) {
    commit('TOGGLE_SIDEBAR')
  },
  closeSideBar({ commit }, { withoutAnimation }) {
    commit('CLOSE_SIDEBAR', withoutAnimation)
  },
  toggleDevice({ commit }, device) {
    commit('TOGGLE_DEVICE', device)
  },
  setSize({ commit }, size) {
    commit('SET_SIZE', size)
  },
  toggleSideBarHide({ commit }, status) {
    commit('SET_SIDEBAR_HIDE', status)
  },
  toggleChangeLoading({ commit }, status) {
    commit('SET_APP_LOADING', status)
  },
  toggleHasRole({ commit }, status) {
    commit('SET_HAS_ROLE', status)
  },
  toggleUkeyStatus({ commit }, status) {
    commit('SET_UKEY_STATUS', status)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
